/* Project cyclecounter02
PIC16F877A
*/

#include <pic.h>
#include "font7s.h"


__CONFIG(
PWRTEN
& BORDIS
& UNPROTECT
& WDTDIS
& LVPDIS
& HS
);
__IDLOC(F877);

#define LED1 RB5
#define LED2 RB4
#define LED3 RB3
#define LED4 RB2
#define LED5 RB1
#define LED6 RB0

unsigned char dig7;
unsigned char ptn7[9];
unsigned char sel7[] = {
0b00000001,
0b00000010,
0b00000100,
0b00001000,
0b00010000,
0b00100000
};
unsigned char a[8];
unsigned char x[8];
unsigned char d, i, t;
unsigned char a1, a2, a3, a4, a5;
unsigned int b, n, N;


void init(void){

dig7 = 0;
ptn7[0] = 0b01011111; // 0
ptn7[1] = 0b01011111;
ptn7[2] = 0b01011111;
ptn7[3] = 0b01011111;
ptn7[4] = 0b01011111;
ptn7[5] = 0b01011111;
ptn7[6] = 0b01011111;
ptn7[7] = 0b01011111;
ptn7[8] = FONT7S_BLANK; // blank
n = 0;
N = 2499;
}


void putui(unsigned int b){

for(i=0; i < 9; i++){
x[i] = 0;

b = a[i]*100 + b;
x[i] = b % 10;
ptn7[i] = font7s[x[i]];
b /= 10;
}
if(x[6] == 0) {
ptn7[6] = FONT7S_BLANK;
if(x[5] == 0)
ptn7[5] = FONT7S_BLANK;
}
}


void interrupt entry(void){

if(TMR1IF){
TMR1IF = 0;
n ++;

PORTB &= ~sel7[dig7];
dig7 ++;
if(dig7 > 5) dig7 = 0;

PORTD = ptn7[dig7 + 1];
PORTB |= sel7[dig7];



if(n > N){
n = 0;

for(i=0; i<9; i++){
ptn7[i] = FONT7S_BLANK;
}

b = TMR0;
a[0] = a1;
a[1] = a2;
a[2] = a3;
a[3] = a4;
a[4] = a5;
a[5] = 0;
a[6] = 0;
a[7] = 0;
a[8] = 0;

putui(b);

a1 = 0; a2 = 0; a3 = 0; a4 = 0; a5 = 0;
TMR0 = 0;

for(i=0; i<1; i++){
for(i=0; i<10; i++){
NOP();
}
}

}
}

TMR1H = 252; TMR1L = 91;
}




void main(void){

CMCON = 0b00000111;
T1CON = 0b00000001;
PIE1 = 0b00000001;
INTCON = 0b01000000;
TMR1IF = 0;
ei();

OPTION = 0b10100000;

PORTA = 0;
TRISA = 0b00010000;

PORTB = 0;
TRISB = 0b00000000;
PORTD = 0;
TRISD = 0b00000000;
PORTC = 0;
TRISC = 0b00000000;
PORTE = 0;
TRISE = 0b00000000;

a1 = 0; a2 = 0;
a3 = 0; a4 = 0; a5 = 0;


TMR1H = 252;
TMR1L = 91;
init();
TMR0 = 0;

while(1){

while(TMR0 < 49);
a1 ++;
if(a1 > 9) (a2 ++, a1 = 0);
if(a2 > 9) (a3 ++, a2 = 0);
if(a3 > 9) (a4 ++, a3 = 0);
if(a4 > 9) (a5 ++, a4 = 0);

TMR0 = 0;
}
}


          戻る